package com.candy.control;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteCookie")
public class DeleteCookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获取所有的cookie
        Cookie[] cookies = req.getCookies();
//        遍历所有 的Cookie
        for (Cookie cookie : cookies) {
//            所有cookie的name为username
            if (cookie.getName().equals("username")) {
//                则删除对应的cookie
                cookie.setMaxAge(0);
                resp.addCookie(cookie);
            }

        }
        resp.getWriter().println("success!");
    }
}
